<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0 auto;
        }

        .box {
            display: none;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: gray;
            opacity: 0.5;
            position: fixed;
        }
        .w {
            padding-top: 20px;
            top: 50%;
            left: 50%;
            text-align: center;
            position: fixed;
            transform: translate(-50%, -50%);
            width: 300px;
            height: 200px;
            background-color: white;
        }
        .username {
            margin-top: 30px;
            margin-bottom: 20px;
        }

        .pas {
            margin-bottom: 20px;
        }

        .clo {
            top: 0;
            left: 258px;
            width: 35px;
            position: absolute;
            color: black;
        }
    </style>
</head>
<body>
    <div class="btn" onclick="show()">
        <a href="javascript:;" class="li">点击弹出模态框</a>
    </div>
    <div class="box">
        <div class="w">
            <h3>登录</h3>
            <div class="clo">关闭</div>
            <form action="javascript:;">
                <label for="username">用户名:</label>
                <input type="text" class="username"><br>
                <label for="pas">密 码:</label>
                <input type="text" class="pas"><br>
                <input type="submit" value="提交">
            </form>
        </div>
    </div>
    <script>
        var btn = document.querySelector('.btn'); 
        var w = document.querySelector('.w'); 
        var box = document.querySelector('.box');
        function show() {
            box.style.display = 'block';
            btn.style.display = 'none';
        }
        var clo = document.querySelector('.clo');
        clo.addEventListener('click', function() {
            box.style.display = 'none';
            btn.style.display = 'block';
            console.log('clo');
        })
        var flag = false;
        var x = 0, y = 0;
        w.addEventListener('mousedown', function(e) {
            flag = true;
            x = e.pageX - this.offsetLeft;
            y = e.pageY - this.offsetTop;
        })
        w.addEventListener('mouseup', function() {flag = false;})
        w.addEventListener('mousemove', function(e) {
            if (!flag) return;
            this.style.top = e.pageY - y + 'px';
            this.style.left = e.pageX - x + 'px';
        })

    </script>
</body>
</html>